package com.aaa.vegetablemarketexpress.controller;

import com.aaa.vegetablemarketexpress.entity.Grade;
import com.aaa.vegetablemarketexpress.service.GradeService;
import com.aaa.vegetablemarketexpress.util.Page;
import com.aaa.vegetablemarketexpress.util.Result;
import com.aaa.vegetablemarketexpress.util.ResultUtil;
import com.github.pagehelper.PageInfo;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @FileName: GradeController
 * @Description: 商品等级控制器
 * @Author:
 * @CreateTime: 2025/9/9
 * @Version: 1.0.0
 */
@RestController
@RequestMapping("/grade")
@Log4j2
public class GradeController {

    @Resource
    private GradeService gradeService;

    /**
     * 获取等级列表
     */
    @PostMapping("/list")
    public Result list(@RequestBody Grade grade) {
        log.info("开始查询等级列表");
        List<Grade> list = gradeService.selectGradeList(grade);
        log.info("查询等级列表结束，共查询到{}条记录", list.size());
        return ResultUtil.success(list);
    }

    /**
     * 分页获取等级列表
     */
    @PostMapping("/listByPage")
    public Result listByPage(@RequestBody Page<Grade> page) {
        log.info("开始执行等级分页查询");
        Integer pageNo = page.getPageNo();
        Integer pageSize = page.getPageSize();

        if (pageNo == null || pageSize == null || pageNo <= 0 || pageSize <= 0) {
            return ResultUtil.failed("分页参数不正确");
        }

        com.github.pagehelper.PageHelper.startPage(pageNo, pageSize);
        List<Grade> list = gradeService.selectGradeList(page.getData());
        PageInfo<Grade> pageInfo = new PageInfo<>(list);
        log.info("等级分页查询结束，共查询到{}条记录", pageInfo.getTotal());
        return ResultUtil.success(pageInfo);
    }

    /**
     * 获取所有正常状态的等级（用于下拉选择）
     */
    @GetMapping("/listAllNormal")
    public Result listAllNormal() {
        log.info("开始查询所有正常状态的等级");
        List<Grade> list = gradeService.selectAllNormalGrade();
        log.info("查询所有正常状态的等级结束，共查询到{}条记录", list.size());
        return ResultUtil.success(list);
    }

    /**
     * 统计等级数量
     */
    @PostMapping("/count")
    public Result count(@RequestBody Grade grade) {
        log.info("开始统计等级数量");
        int count = gradeService.countGrade(grade);
        log.info("统计等级数量结束，统计结果为{}", count);
        return ResultUtil.success(count);
    }

    /**
     * 获取等级详细信息
     */
    @GetMapping("/{gradeId}")
    public Result getInfo(@PathVariable("gradeId") Long gradeId) {
        log.info("开始查询等级详细信息，等级ID: {}", gradeId);
        Grade grade = gradeService.selectGradeById(gradeId);
        if (grade == null) {
            log.warn("未找到ID为{}的等级信息", gradeId);
            return ResultUtil.failed("未找到对应的等级信息");
        }
        log.info("查询等级详细信息结束");
        return ResultUtil.success(grade);
    }

    /**
     * 新增等级
     */
    @PostMapping("add")
    public Result add(@RequestBody Grade grade) {
        log.info("开始新增等级，等级名称: {}", grade.getGradeName());
        try {
            int result = gradeService.insertGrade(grade);
            if (result > 0) {
                log.info("新增等级成功，等级ID: {}", grade.getGradeId());
                return ResultUtil.success("新增等级成功");
            } else {
                log.warn("新增等级失败");
                return ResultUtil.failed("新增等级失败");
            }
        } catch (Exception e) {
            log.error("新增等级时发生异常", e);
            return ResultUtil.failed("新增等级失败: " + e.getMessage());
        }
    }

    /**
     * 修改等级
     */
    @PostMapping("edit")
    public Result edit(@RequestBody Grade grade) {
        log.info("开始修改等级，等级ID: {}", grade.getGradeId());
        if (grade.getGradeId() == null) {
            return ResultUtil.failed("等级ID不能为空");
        }
        try {
            int result = gradeService.updateGrade(grade);
            if (result > 0) {
                log.info("修改等级成功，等级ID: {}", grade.getGradeId());
                return ResultUtil.success("修改等级成功");
            } else {
                log.warn("修改等级失败");
                return ResultUtil.failed("修改等级失败");
            }
        } catch (Exception e) {
            log.error("修改等级时发生异常", e);
            return ResultUtil.failed("修改等级失败: " + e.getMessage());
        }
    }

    /**
     * 删除等级
     */
    @DeleteMapping("/{gradeId}")
    public Result remove(@PathVariable Long gradeId) {
        log.info("开始删除等级，等级ID: {}", gradeId);
        if (gradeId == null) {
            return ResultUtil.failed("等级ID不能为空");
        }
        try {
            int result = gradeService.deleteGradeById(gradeId);
            if (result > 0) {
                log.info("删除等级成功，等级ID: {}", gradeId);
                return ResultUtil.success("删除等级成功");
            } else {
                log.warn("删除等级失败");
                return ResultUtil.failed("删除等级失败");
            }
        } catch (Exception e) {
            log.error("删除等级时发生异常", e);
            return ResultUtil.failed("删除等级失败: " + e.getMessage());
        }
    }

    /**
     * 批量删除等级
     */
    @DeleteMapping("/batch/{gradeIds}")
    public Result removeBatch(@PathVariable Long[] gradeIds) {
        log.info("开始批量删除等级，等级IDs: {}", (Object) gradeIds);
        if (gradeIds == null || gradeIds.length == 0) {
            return ResultUtil.failed("请选择要删除的等级");
        }
        try {
            int result = gradeService.deleteGradeByIds(gradeIds);
            if (result > 0) {
                log.info("批量删除等级成功，删除数量: {}", result);
                return ResultUtil.success("批量删除等级成功");
            } else {
                log.warn("批量删除等级失败");
                return ResultUtil.failed("批量删除等级失败");
            }
        } catch (Exception e) {
            log.error("批量删除等级时发生异常", e);
            return ResultUtil.failed("批量删除等级失败: " + e.getMessage());
        }
    }

    /**
     * 校验等级名称是否唯一
     */
    @GetMapping("/checkGradeNameUnique")
    public Result checkGradeNameUnique(@RequestBody Grade grade) {
        log.info("开始校验等级名称唯一性，等级名称: {}", grade.getGradeName());
        String result = gradeService.checkGradeNameUnique(grade);
        log.info("校验等级名称唯一性结束，结果: {}", result);
        return ResultUtil.success(result);
    }

    /**
     * 更新等级状态
     */
    @PutMapping("/changeStatus")
    public Result changeStatus(@RequestBody Grade grade) {
        log.info("开始更新等级状态，等级ID: {}, 状态: {}", grade.getGradeId(), grade.getStatus());
        if (grade.getGradeId() == null) {
            return ResultUtil.failed("等级ID不能为空");
        }
        try {
            int result = gradeService.updateGradeStatus(grade.getGradeId(), String.valueOf(grade.getStatus()));
            if (result > 0) {
                log.info("更新等级状态成功，等级ID: {}", grade.getGradeId());
                return ResultUtil.success("更新等级状态成功");
            } else {
                log.warn("更新等级状态失败");
                return ResultUtil.failed("更新等级状态失败");
            }
        } catch (Exception e) {
            log.error("更新等级状态时发生异常", e);
            return ResultUtil.failed("更新等级状态失败: " + e.getMessage());
        }
    }
}

